package com.kly.chatter.api.application.service.impl;

import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Maps;
import com.jsoniter.output.JsonStream;
import com.kly.chatter.api.application.controller.user.req.MessageReq;
import com.kly.chatter.api.application.service.CmFirstMessageRecordService;
import com.kly.chatter.api.application.service.UsersService;
import com.kly.chatter.db.dao.ChatterOnlineListMatchRecordDao;
import com.kly.chatter.db.dao.CmFirstMessageRecordDao;
import com.kly.chatter.db.entity.ChatterOnlineListMatchRecordEntity;
import com.kly.chatter.db.entity.CmFirstMessageRecordEntity;
import com.kly.user.dto.AddWlmReq;
import com.kly.user.enums.LikeEnum;
import com.kly.user.service.UserMsgService;
import com.kly.user.service.UserService;
import com.kly.user.service.WlmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class CmFirstMessageRecordServiceImpl implements CmFirstMessageRecordService {


    @Autowired
    private CmFirstMessageRecordDao cmFirstMessageRecordDao;

    @Resource
    private UserService userService;

    @Resource
    private UserMsgService userMsgService;

    @Resource
    WlmService wlmService;

    @Autowired
    private ChatterOnlineListMatchRecordDao chatterOnlineListMatchRecordDao;

    @Override
    public void addRecord(MessageReq messageReq) {
        Boolean haveConversation = userService.haveConversationByModel(messageReq.getChatterUserCode(), messageReq.getModelUserCode(), messageReq.getUserCode());
        if (haveConversation) {
            final HashMap<String, Object> params = Maps.newHashMap();
            params.put("chatterCode", messageReq.getChatterUserCode());
            params.put("modelCode", messageReq.getModelUserCode());
            params.put("userCode", messageReq.getUserCode());
            int recordCount = cmFirstMessageRecordDao.queryTotal(params);
            if (recordCount == 0) {
                CmFirstMessageRecordEntity cmFirstMessageRecordEntity = new CmFirstMessageRecordEntity();
                cmFirstMessageRecordEntity.setChatterCode(messageReq.getChatterUserCode());
                cmFirstMessageRecordEntity.setModelCode(messageReq.getModelUserCode());
                cmFirstMessageRecordEntity.setUserCode(messageReq.getUserCode());
                cmFirstMessageRecordDao.insert(cmFirstMessageRecordEntity);
                userMsgService.sendFlashChatFlag(messageReq.getModelUserCode(), messageReq.getUserCode());

                // 添加flashchat标记
                AddWlmReq addWlmReq = new AddWlmReq();
                addWlmReq.setLikeType(LikeEnum.LIKE_EACH_OTHER.getType());
                addWlmReq.setUserCode(messageReq.getModelUserCode());
                addWlmReq.setUserCodeFriend(messageReq.getUserCode());
                addWlmReq.setFlashchatFlag(Boolean.TRUE);
                wlmService.connectByFlashChat(addWlmReq);
            }
        }

        try {
            addNewRelationLog(messageReq);
        } catch (Exception e) {
            log.error("==addNewRelationLog 异常  添加记录==,params:{}", JsonStream.serialize(messageReq), e);
        }

    }


    private void addNewRelationLog(MessageReq messageReq) {
        log.info("==addNewRelationLog  开始==");
        List<ChatterOnlineListMatchRecordEntity> chatterOnlineListMatchRecordEntities = chatterOnlineListMatchRecordDao.selectTodayByChatter(messageReq.getChatterUserCode());
        if (CollectionUtils.isEmpty(chatterOnlineListMatchRecordEntities)) {
            log.info("==addNewRelationLog 当前无记录  添加记录==");
            chatterOnlineListMatchRecordDao.insert(new ChatterOnlineListMatchRecordEntity().setCreateDt(new Date()).setTotalCnt(1).setUsedCnt(1).setModelCode(messageReq.getModelUserCode()).setChatterCode(messageReq.getChatterUserCode()));
            log.info("==addNewRelationLog 当前无记录  添加记录 添加完成==");
            return;
        }

        log.info("==addNewRelationLog 当前有记录，开始查找对应的记录==");
        boolean findRecord = false;
        ChatterOnlineListMatchRecordEntity target = null;
        for (ChatterOnlineListMatchRecordEntity chatterOnlineListMatchRecordEntity : chatterOnlineListMatchRecordEntities) {
            boolean isRightOne = chatterOnlineListMatchRecordEntity.getModelCode().equals(messageReq.getModelUserCode());
            if (isRightOne) {
                log.info("==addNewRelationLog 当前有记==");
                findRecord = true;
                target = chatterOnlineListMatchRecordEntity;

            } else {
                log.info("==addNewRelationLog 当前有记录，更新失败==");
            }
        }

        if (findRecord) {
            log.info("==addNewRelationLog 当前有记 更新==");
            target.setTotalCnt(target.getTotalCnt() + 1).setUsedCnt(target.getUsedCnt() + 1);
            chatterOnlineListMatchRecordDao.updateByPrimaryKey(target);
        } else {
            log.info("==addNewRelationLog 当前没有记 插入新的model记录==");
            chatterOnlineListMatchRecordDao.insert(new ChatterOnlineListMatchRecordEntity().setCreateDt(new Date()).setTotalCnt(1).setUsedCnt(1).setModelCode(messageReq.getModelUserCode()).setChatterCode(messageReq.getChatterUserCode()));
        }


    }
}
